<?php
namespace Admin\Model;
use Think\Model;

class UserModel extends Model {

	//ajax验证
	public function Login() {
		if (IS_POST) {

			$param = I('post.param'); //字段值
			$name = I('post.name'); //字段
			$require = $this->where(array($name => $param))->field($name . ',lock')->find();

			if ($require['lock'] == 1) {
				$arr['status'] = 0;
				$arr['info'] = "帐户已被禁用";
				$arr['status'] = 'n';
				return $arr;
			}
			if ($require) {
				$arr['status'] = 0;
				$arr['info'] = "验证通过";
				$arr['status'] = 'y';
			} else {
				$arr['status'] = 0;
				$arr['info'] = "帐号不存在";
				$arr['status'] = 'n';
			}
		} else {

			$arr['status'] = 0;
			$arr['info'] = "非法请求";
			$arr['status'] = 'n';
		}
		return $arr;
	}
	//用户登陆验证
	public function UserLogin() {

		$username = I('post.username');
		$password = md5(I('post.password'));
		//验证验证码
		$code = I('post.code');
		$verify = new \Think\Verify();
		if (!$verify->check($code, '1')) {
			$msg['status'] = 0;
			$msg['msg'] = '验证码错误';
			return $msg;
		}
		$require = $this->where(array('username' => $username, 'password' => $password))
			->find();
		if ($require['lock'] == 1) {
			$msg['status'] = 0;
			$msg['msg'] = '帐号已被禁用!';
			return $msg;
		}
		if ($require) {
			$msg['info'] = $require;
			$msg['status'] = 1;
			$msg['msg'] = '登陆成功';
			$msg['url'] = U('Admin/Index/index');
		} else {
			$msg['status'] = 0;
			$msg['msg'] = '帐号或密码错误!';
		}
		return $msg;

	}
	/*
		$id int 用户id
		$nid int 已结算车型id
		return 用户工资信息
	*/
	public function getUserMoney($id, $nid) {
		if (!$id) {
			return false;
		}
		if (!$nid) {
			$map['uid'] = $id;
			$map['jieshan_zhuangtai'] = 1;
		} else {
			$map['jieshan_zhuangtai'] = 2;
			$map['id'] = array('in', $nid);
		}

		$getinfo = M(GetModelTable(31))->where($map)->field(true)->select();
		for ($i = 0; $i < count($getinfo); $i++) {
			//订单信息
			$getinfo[$i]['xunpan'] = M(GetModelTable(30))->where(array('id' => $getinfo[$i]['queding']))->find();
			//询盘信息
			$getinfo[$i]['xunpan']['tianjia'] = M(GetModelTable(19))->where(array('id' => $getinfo[$i]['xunpan']['queding']))->find();
			//车型利润

			// 车辆全款
			$cheliangquankuan = $getinfo[$i]['xunpan']['cheliangquankuan'];
			//底盘价格
			$dipanjiage = $getinfo[$i]['xunpan']['dipanjiage'];
			//上装价格
			$shangzhuangjiage = $getinfo[$i]['xunpan']['shangzhuangjiage'];
			//送车费
			$songcheyunfei = $getinfo[$i]['songcheyunfei'];

			$count = $dipanjiage + $shangzhuangjiage + $songcheyunfei;
			//每辆车型的利润
			$getinfo[$i]['count'] = $cheliangquankuan - $count;

			//收入及支出明细
			$suoshu = $getinfo[$i]['xunpan']['hetongbianhao'];
			$feiyong[$i] = M(GetModelTable(35))->where(array('suoshuhetongdanhao' => $suoshu))->select();
			//该订单总收入
			$feiyong[$i]['income'] = M(GetModelTable(35))->where(array('suoshuhetongdanhao' => $suoshu, 'leibie' => '收入'))->Sum('jine');
			//该订单总支出
			$feiyong[$i]['expenditure'] = M(GetModelTable(35))->where(array('suoshuhetongdanhao' => $suoshu, 'leibie' => '支出'))->Sum('jine');
		}

		//统计总合
		for ($m = 0; $m < count($getinfo); $m++) {
			$array_sum[] = $getinfo[$m]['count'];
			//统计总收入
			$income[] = $feiyong[$m]['income'];
			//统计总支出
			$expenditure[] = $feiyong[$m]['expenditure'];
		}

		//提成比例
		$users = M('user')->where(array('id' => getuserid()))->field('password', true)->find();

		$sum = array(
			'nomey' => array_sum($array_sum) ? array_sum($array_sum) : 0, //总利润
			'count' => count($getinfo), //台数
			'dixin' => $users['dixin'], //底薪
			'ticheng' => $users['ticheng'], //提成比例
			'username' => $users['username'], //用户名
			'uid' => $users['id'],
			'income' => array_sum($income) ? array_sum($income) : 0, //总收入
			'expenditure' => array_sum($expenditure) ? array_sum($expenditure) : 0, //总支出

		);
		return array('sum' => $sum, 'getinfo' => $getinfo, 'feiyong' => $feiyong);
	}
	/*生成二维码*/
	public function code($url) {
		//引用ThinkPHP/Extend/Vendor/phpqrcode/phpqrcode.php
		vendor("phpqrcode.phpqrcode");
		//@ $url 要生成的网址
		$data = $url;
		// 纠错级别：L、M、Q、H
		$level = 'L';
		// 点的大小：1到10,用于手机端4就可以了
		$size = 10;
		// 下面注释了把二维码图片保存到本地的代码,如果要保存图片,用$fileName替换第二个参数false
		// $path = __ROOT__."/Uploads/";
		// 生成的文件名
		// $fileName = $path.$size.'.png';
		// echo $fileName;
		\QRcode::png($data, false, $level, $size);

	}
}